
var ref = require('ref');
var ffi = require('ffi');


function HardDisk () {
    this.currentHardDrive = []; //当前的硬盘
    this.nextHardDrive = []; //下次的硬盘
    this.chkdsk = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    this.currentDrivers = 0;
    this.nextDrivers= 0 ;
    this.kernel32 = new ffi.Library('kernel32', {
        'GetLogicalDrives': ['int32', [ ]],
        'GetDriveTypeW': ['uint32', ['string']],
        'GetDiskFreeSpaceExW': ['int32', ['string', ref.refType('uint64'), ref.refType('uint64'), ref.refType('uint64')]]
    });
}

HardDisk.prototype.InitHardDisk = function (callbackmobileAdd, callbackLocaldiskAdd, callbackSub) {
    this.nextDrivers = this.kernel32.GetLogicalDrives()
    if( this.currentDrivers === this.nextDrivers){
        this.currentDrivers = this.nextDrivers
        this.currentHardDrive = [...this.nextHardDrive]
    }else{
        var hardDiskMedian = []
        this.nextHardDrive =[...this.GetChkDsk(this.nextDrivers)]  //获得下个盘符数组
        hardDiskMedian = [...this.nextHardDrive]
        // 盘符减少
        this.currentHardDrive.map(item => {
            if( this.nextHardDrive.indexOf(item) === -1){
                console.log('盘符减少::',item)
                callbackSub(item)
            }
        })
        //盘符增加
        this.nextHardDrive.map(item => {
            if( this.currentHardDrive.indexOf(item) === -1){
                console.log('盘符增加::',item)
                if(this.GetMobileChkDsk(item)){
                    callbackmobileAdd(item)
                }else{
                    callbackLocaldiskAdd(item)
                }
            }
        })
        this.currentDrivers = this.nextDrivers
        this.currentHardDrive = [...hardDiskMedian]
       
    }
}

HardDisk.prototype.GetDiskFreeSpaceExW = function (driveLetter,callback) {
   var  str = driveLetter + ":\\"
   var  freeBytesAvailable = ref.alloc('uint64');
   var  totalNumberOfBytes = ref.alloc('uint64');
   var  totalNumberOfFreeBytes = ref.alloc('uint64');
   this.kernel32.GetDiskFreeSpaceExW(ucs2TransfortoString(str), freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes);
   var  freeBytesAvailable = freeBytesAvailable.deref();
   var totalNumberOfBytes = totalNumberOfBytes.deref();
   var _totalNumberOfBytes = (totalNumberOfBytes / 1024 / 1024 / 1024).toFixed(2)
   var _freeBytesAvailable = (freeBytesAvailable / 1024 / 1024 / 1024).toFixed(2)
   callback({
    "diskTotal": _totalNumberOfBytes,
    "diskfreeTotal": _freeBytesAvailable
   })
}
//获取盘符
HardDisk.prototype.GetChkDsk = function (str) {
    let chkDskStr = str.toString(2).split("").reverse()
    let chkDskArr = [] //盘符数组
    chkDskStr.forEach((item, index) => {
        if(item === '1'){
            chkDskArr.push(this.chkdsk[index])
        }
    });
    return chkDskArr
}

//获取新增移动盘
HardDisk.prototype.GetMobileChkDsk = function (driveLetter) {
    var str = driveLetter + ":\\"
    var driverType = this.kernel32.GetDriveTypeW(ucs2TransfortoString(str));
    if(driverType === 2) {
       return true
    }
    return false
}